<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import= "model.filemanager.FileManager" %>
<%@ page import= "model.filemanager.MediaFile" %>
<%@ page import= "view.*" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import ="com.google.appengine.api.users.UserService" %>
<%@ page import ="com.google.appengine.api.users.UserServiceFactory" %>

<% 
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String type = request.getParameter("type");
String searchP = request.getParameter("sP");
String searchT = request.getParameter("sT");
String sort = request.getParameter("sort");
String desc = request.getParameter("desc");
String offset = request.getParameter("offset");
String listP = request.getParameter("list");
	if (type != null && type.length() == 0)
		type = null;
	if (searchP != null && searchP.length() == 0)
		searchP = null;
	if (searchT != null && searchT.length() == 0)
		searchT = null;
	if (sort != null && sort.length() == 0)
		sort = null;
	if (desc != null && desc.length() == 0)
		desc = null;
	if (offset != null && offset.length() == 0)
		offset = null;
	if (listP != null && listP.length() == 0)
		listP = null;
	MediaInfo mediaInfo = new MediaInfo();
	FileManager fm = new FileManager(user.getEmail());
	MediaFile[] files = null;
	Integer[] totalCount = new Integer[1];
	boolean descending = false;
	int offSet = 0;
	long listKey = -1;
	if (desc != null)
	{
		if (desc.length() == 1)
		{
			char a = desc.charAt(0);
			if (a == 'y')
				descending = true;
		}
	}
	
	if (listP != null)
	{
		try
		{
			listKey = Integer.parseInt(listP);
		}
		catch(NumberFormatException name)
		{
			listKey = -1;
		}
	}
	
	if (offset != null)
	{
		try
		{
			offSet = Integer.parseInt(offset);
		}
		catch(NumberFormatException name)
		{
			offSet = 0;
		}
	}

	if (type != null && (type.contentEquals("image") || type.contentEquals("video") || type.contentEquals("audio")))
	{
		files = fm.SearchForFiles(type,searchP, searchT, sort,listKey,descending,offSet,10,totalCount).toArray(new MediaFile[0]);	
	}
	else
	{
		files = fm.SearchForFiles(null,searchP, searchT, sort,listKey,descending,offSet,10,totalCount).toArray(new MediaFile[0]);
	}

%>
	 	<div id="tableDataDiv">
	 	<%= mediaInfo.GenerateTable(type, files, listP) %>
	 	</div>
	 	<div id="pagerDiv">
	 	<%= mediaInfo.GeneratePager(totalCount[0], offSet) %>
	 	</div>